"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

require("antd/es/table/style");

var _table = _interopRequireDefault(require("antd/es/table"));

require("antd/es/drawer/style");

var _drawer = _interopRequireDefault(require("antd/es/drawer"));

require("antd/es/button/style");

var _button = _interopRequireDefault(require("antd/es/button"));

var _ArrowDownOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowDownOutlined"));

var _ArrowUpOutlined2 = _interopRequireDefault(require("@ant-design/icons/lib/icons/ArrowUpOutlined"));

require("antd/es/popconfirm/style");

var _popconfirm = _interopRequireDefault(require("antd/es/popconfirm"));

var _react = _interopRequireWildcard(require("react"));

var _index = _interopRequireDefault(require("../../index"));

var _hooks = require("../../../hooks");

var _utils = require("../../../utils");

var _ErrorMessage = _interopRequireDefault(require("../../RenderField/ErrorMessage"));

var _excluded = ["buttons"],
    _excluded2 = ["pagination"];

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var FIELD_LENGTH = 170;

var DrawerList = function DrawerList(_ref) {
  var _extends2;

  var _ref$displayList = _ref.displayList,
      displayList = _ref$displayList === void 0 ? [] : _ref$displayList,
      dataPath = _ref.dataPath,
      children = _ref.children,
      deleteItem = _ref.deleteItem,
      addItem = _ref.addItem,
      moveItemDown = _ref.moveItemDown,
      moveItemUp = _ref.moveItemUp,
      flatten = _ref.flatten,
      errorFields = _ref.errorFields,
      getFieldsProps = _ref.getFieldsProps,
      schema = _ref.schema,
      changeList = _ref.changeList,
      listData = _ref.listData;
  var _schema$props = schema.props,
      props = _schema$props === void 0 ? {} : _schema$props,
      _schema$itemProps = schema.itemProps,
      itemProps = _schema$itemProps === void 0 ? {} : _schema$itemProps;

  var buttons = itemProps.buttons,
      columnProps = _objectWithoutProperties(itemProps, _excluded);

  var _props$pagination = props.pagination,
      pagination = _props$pagination === void 0 ? {} : _props$pagination,
      rest = _objectWithoutProperties(props, _excluded2);

  var paginationConfig = pagination && _objectSpread({
    size: 'small',
    hideOnSinglePage: true
  }, pagination);

  var currentIndex = (0, _react.useRef)(-1);

  var _useSet = (0, _hooks.useSet)({
    showDrawer: false
  }),
      _useSet2 = _slicedToArray(_useSet, 2),
      state = _useSet2[0],
      setState = _useSet2[1];

  var showDrawer = state.showDrawer;
  var dataSource = displayList.map(function (item, index) {
    return _objectSpread(_objectSpread({}, item), {}, {
      $idx: index
    });
  });
  var columns = children.map(function (child) {
    var item = flatten[child];
    var schema = item && item.schema || {};

    var _dataIndex = (0, _utils.getKeyFromPath)(child);

    return _objectSpread({
      dataIndex: _dataIndex,
      title: schema.required ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("span", {
        className: "fr-label-required"
      }, " *"), /*#__PURE__*/_react.default.createElement("span", null, schema.title)) : schema.title,
      width: FIELD_LENGTH,
      render: function render(value, record) {
        var childPath = (0, _utils.getDataPath)(child, [record.$idx]);
        var errorObj = errorFields.find(function (item) {
          return item.name == childPath;
        }) || {}; //TODO: 万一error在更深的层，这个办法是find不到的，会展示那一行没有提示。可以整一行加一个红线的方式处理

        return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("div", null, (0, _utils.getDisplayValue)(value, schema)), errorObj.error && /*#__PURE__*/_react.default.createElement(_ErrorMessage.default, {
          message: errorObj.error,
          schema: schema
        }));
      }
    }, columnProps);
  });
  columns.push({
    title: '操作',
    key: '$action',
    fixed: 'right',
    width: 120,
    render: function render(value, record, idx) {
      var index = value && value.$idx || 0;
      return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("a", {
        onClick: function onClick() {
          return openDrawer(index);
        }
      }, "\u7F16\u8F91"), !props.hideDelete && /*#__PURE__*/_react.default.createElement(_popconfirm.default, {
        title: "\u786E\u5B9A\u5220\u9664?",
        onConfirm: function onConfirm() {
          return deleteItem(index);
        },
        okText: "\u786E\u5B9A",
        cancelText: "\u53D6\u6D88"
      }, /*#__PURE__*/_react.default.createElement("a", null, "\u5220\u9664")), !props.hideMove && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ArrowUpOutlined2.default, {
        style: {
          color: '#1890ff',
          fontSize: 16,
          marginLeft: 4
        },
        onClick: function onClick() {
          return moveItemUp(idx);
        }
      }), /*#__PURE__*/_react.default.createElement(_ArrowDownOutlined2.default, {
        style: {
          color: '#1890ff',
          fontSize: 16,
          marginLeft: 4
        },
        onClick: function onClick() {
          return moveItemDown(idx);
        }
      })));
    }
  });
  var fieldsProps = getFieldsProps(currentIndex.current);

  var openDrawer = function openDrawer(index) {
    currentIndex.current = index;
    setState({
      showDrawer: true
    });
  };

  var closeDrawer = function closeDrawer() {
    currentIndex.current = -1;
    setState({
      showDrawer: false
    });
  };

  var handleAdd = function handleAdd() {
    var newIndex = addItem();
    openDrawer(newIndex);
  };

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", {
    className: "w-100 mb2 tr"
  }, !props.hideAdd && /*#__PURE__*/_react.default.createElement(_button.default, {
    type: "primary",
    size: "small",
    onClick: handleAdd
  }, "\u65B0\u589E"), Array.isArray(props.buttons) ? props.buttons.map(function (item, idx) {
    var callback = item.callback,
        text = item.text,
        html = item.html;

    var onClick = function onClick() {
      console.log({
        value: listData,
        onChange: changeList,
        schema: schema
      });
    };

    if (typeof window[callback] === 'function') {
      onClick = function onClick() {
        window[callback]({
          value: listData,
          onChange: changeList,
          schema: schema
        });
      };
    }

    return /*#__PURE__*/_react.default.createElement(_button.default, {
      key: idx.toString(),
      style: {
        marginLeft: 8
      },
      size: "small",
      onClick: onClick
    }, /*#__PURE__*/_react.default.createElement("span", {
      dangerouslySetInnerHTML: {
        __html: html || text
      }
    }));
  }) : null), /*#__PURE__*/_react.default.createElement(_drawer.default, {
    width: "600",
    title: "\u7F16\u8F91",
    placement: "right",
    onClose: closeDrawer,
    visible: showDrawer,
    destroyOnClose: true // 必须要加，currentIndex不是一个state，Core不会重新渲染就跪了

  }, /*#__PURE__*/_react.default.createElement("div", {
    className: "fr-container"
  }, /*#__PURE__*/_react.default.createElement(_index.default, fieldsProps))), /*#__PURE__*/_react.default.createElement(_table.default, _extends((_extends2 = {
    size: "small",
    scroll: {
      x: 'max-content'
    },
    columns: columns,
    dataSource: dataSource,
    rowClassName: function rowClassName(record, idx) {
      var index = record && record.$idx;
      var hasError = errorFields.find(function (item) {
        return item.name.indexOf("".concat(dataPath, "[").concat(index, "]")) > -1;
      });
      return hasError ? 'fr-row-error' : '';
    },
    rowKey: "$idx"
  }, _defineProperty(_extends2, "size", "small"), _defineProperty(_extends2, "pagination", paginationConfig), _extends2), rest)));
};

var _default = DrawerList;
exports.default = _default;